package com.youlu.campus.service.platform.impl;

import com.alibaba.fastjson.JSON;

import com.youlu.campus.component.SmsUtil;
import com.youlu.campus.entity.PlatformComplaintConfig;
import com.youlu.campus.entity.PlatformUserComplaint;
import com.youlu.campus.entity.UserInfoExtend;
import com.youlu.campus.service.platform.PlatformComplaintConfigService;
import com.youlu.campus.service.platform.PlatformUserComplaintNotifyService;
import com.youlu.campus.service.sms.SmsServerService;
import com.youlu.campus.service.visitor.UserInfoExtendService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Slf4j
@Service
public class PlatformUserComplaintNotifyServiceImpl implements PlatformUserComplaintNotifyService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private UserInfoExtendService userInfoExtendService;
    @Autowired
    private SmsServerService smsServerService;
    @Autowired
    private SmsUtil smsUtil;
    @Autowired
    private PlatformComplaintConfigService platformComplaintConfigService;

    @Async
    @Override
    public void sendSms(PlatformUserComplaint platformUserComplaint) {
        log.info(":>>> 开始发送用户投诉协商短信:{}", JSON.toJSON(platformUserComplaint));
        String mobile = "";
        if (StringUtils.isBlank(platformUserComplaint.getMobile())) {
            log.error(":>>> 用户投诉手机号为空:{}", platformUserComplaint.getId());
            if (StringUtils.isBlank(platformUserComplaint.getUserId())) {
                log.error(":>>> 用户投诉用户ID为空,匿名投诉:{}", platformUserComplaint.getId());
                return;
            }
            UserInfoExtend extend = userInfoExtendService.findByUserId(platformUserComplaint.getUserId());
            if (Objects.isNull(extend)) {
                log.error(":>>> 用户:{} 的扩展信息为空", platformUserComplaint.getUserId());
                return;
            }
            if (StringUtils.isBlank(extend.getMobile())) {
                log.error(":>>> 用户:{} 的扩展信息中手机号为空", platformUserComplaint.getUserId());
                return;
            }
            mobile = extend.getMobile();
        } else {
            mobile = platformUserComplaint.getMobile();
        }
        PlatformComplaintConfig platformComplaintConfig = platformComplaintConfigService.find();
        if (Objects.nonNull(platformComplaintConfig)) {
            this.sendSms(platformComplaintConfig.getSmsSign(), mobile, platformComplaintConfig.getTemplateId().toString());
        }

    }

        private void sendSms(String sign,
                         String mobile,
                         String templateId1) {
        log.info(":>>> 开始发送短信:{},{},{}", sign, mobile, templateId1);
        try {
            String[] params = new String[]{"尊敬的用户"};
            smsUtil.sendSmsWithContent(sign, mobile, Integer.valueOf(templateId1), params);
        } catch (Exception e) {
            log.error(":>>> 发送短信错误:{}", e);
        }
    }
}
